package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.backend.controller.base.BaseController;
import com.jrtc.base.entity.bo.Doctor;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.DoctorInformationVo;
import com.jrtc.base.entity.vo.InformationVo;
import com.jrtc.base.entity.vo.ReserveDoctorVo;
import com.jrtc.base.entity.vo.DoctorLoginVo;
import com.jrtc.base.util.KeyUtils;
import com.jrtc.service.DoctorService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 医生表(Doctor)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("doctor")
public class DoctorController extends BaseController
{
    /**
     * 服务对象
     */
    @Resource
    private DoctorService doctorService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Doctor selectOne(Integer id) {
        return this.doctorService.queryById(id);
    }
    
    @RequestMapping("getDoctorList")
    public ResultDTO getDoctorList(@RequestBody Doctor doctor)
    {
        IPage<Doctor> ipage = doctorService.queryDoctorList(doctor);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertDoctor")
    public ResultDTO insertDoctor(@RequestBody Doctor doctor)
    {
        Doctor tmpDoctor = doctorService.insert(doctor);
        if (tmpDoctor != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getDoctorById")
    public ResultDTO getDoctorById(@RequestBody Map<String, Integer> map)
    {
        int doctorId = map.get("doctorId");
        Doctor doctor = doctorService.queryById(doctorId);
        if (doctor==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(doctor);
    }

    @RequestMapping("updateDoctor")
    public ResultDTO updateDoctor(@RequestBody Doctor doctor)
    {
        if (doctor==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (doctorService.update(doctor)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteDoctor")
    public ResultDTO deleteDoctor(@RequestBody Map<String,Integer> map)
    {
        int doctorId = map.get("doctorId");
        if (doctorService.deleteById(doctorId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 医生后台登录
     * @param doctorLogin
     * @return
     */
    @PostMapping("/doctorLogin")
    public ResultDTO login(@RequestBody DoctorLoginVo doctorLogin) {
        Doctor doctor = doctorService.login(doctorLogin);
        if (doctor == null) {
            return ResultDTOBuilder.failure("01000","账号或密码不正确！");
        }
        return ResultDTOBuilder.success(doctor);
    }

    /**
     * 医生退出登录
     * @param request
     * @return
     */
    @PostMapping("/logOut")
    public ResultDTO logOut(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token != null && !token.equals("")) {
            boolean logOut = doctorService.logOut(token);
            if (logOut)
                return ResultDTOBuilder.success("退出登录成功！");
            else
                return ResultDTOBuilder.failureMsg("您当前未登录！");
        } else {
            return ResultDTOBuilder.failureMsg("未获取到token！");
        }
    }

    /**
     * 新增医生信息
     * */
    @PostMapping("/addDoctor")
    public ResultDTO addDoctor(@RequestBody Doctor doctor){
        int i;
        try {
            i = doctorService.insertDoctor(doctor);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        if (i > 0){
            return ResultDTOBuilder.success("添加成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("添加失败");
        }

//        if (doctor != null && map.get("departmentName") != null && !map.get("departmentName").equals("")){
//            String departmentName = map.get("departmentName").toString();
//            int i;
//            try {
//                i = doctorService.insertDoctor(doctor, departmentName, source, business, file);
//            } catch (Exception e){
//                return ResultDTOBuilder.failureMsg(e.getMessage());
//            }
//            if (i > 0){
//                return ResultDTOBuilder.success("补充成功");
//            }
//            else {
//                return ResultDTOBuilder.failureMsg("补充失败");
//            }
//        }
//        else {
//            return ResultDTOBuilder.failureMsg("缺少必要参数");
//        }
    }

    /**
     * 医生注册文件上传
     * */
    @PostMapping("/uploadFile")
    public ResultDTO uploadFile(List<MultipartFile> file, String type){
        List<String> stringList;
        try {
            stringList = doctorService.uploadFile(file, type);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(stringList);
    }

    /**
     *
     * */
    @PostMapping("/puploadFile")
    public ResultDTO puploadFile(List<MultipartFile> file){
        List<String> stringList;
        try {
            stringList = doctorService.puploadFile(file);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(stringList);
    }

    /**
     * 医生注册文件上传
     * */
    @PostMapping("r")
    public ResultDTO r(List<MultipartFile> file){
        List<String> stringList;
        try {
            stringList = doctorService.puploadFile(file);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(stringList);
    }

    /**
     * 医院就诊挂号医生信息
     * */
    @PostMapping("/selectDoctorById")
    public ResultDTO selectDoctorById(@RequestBody Map<String, Object> map){
        if (map.get("doctorId") != null && !map.get("doctorId").equals("")){
            Integer doctorId = Integer.parseInt(map.get("doctorId").toString());
            ReserveDoctorVo reserveDoctorVo;
            try {
                reserveDoctorVo = doctorService.selectDoctorById(doctorId);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (reserveDoctorVo != null){
                return ResultDTOBuilder.success(reserveDoctorVo);
            }
            else {
                return ResultDTOBuilder.failureMsg("未查到数据");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 医生信息及他的运动处方
     * */
    @PostMapping("/selectDoctorInformationVo")
    public ResultDTO selectDoctorInformationVo(HttpServletRequest request, @RequestBody InformationVo informationVo){
        Doctor loginDoctor;
        try {
            loginDoctor = doctorService.getLoginDoctor(request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        Integer doctorId = Integer.parseInt(loginDoctor.getDoctorId().toString());
        DoctorInformationVo doctorInformationVo;
        try {
            doctorInformationVo = doctorService.selectDoctorInformationVo(doctorId, informationVo);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (doctorInformationVo != null){
            return ResultDTOBuilder.success(doctorInformationVo);
        }
        else {
            return ResultDTOBuilder.failureMsg("未查到数据");
        }
    }

    /**
     * 医生姓名列表
     * */
    @PostMapping("/doctorNameList")
    public ResultDTO doctorNameList(){
        List<Doctor> doctors = doctorService.doctorNameList();
        return ResultDTOBuilder.success(doctors);
    }

    /**
     * 医生添加患者运动处方
     * */
    @PostMapping("/insertPrescription")
    public ResultDTO insertPrescription(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Doctor loginDoctor;
        try {
            loginDoctor = doctorService.getLoginDoctor(request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        Integer id = Integer.parseInt(map.get("id").toString());
        Integer prescriptionId = Integer.parseInt(map.get("prescriptionId").toString());

        int i;
        try {
            i = doctorService.insertPrescription(loginDoctor.getDoctorId(), id, prescriptionId);
        } catch (Exception e){
            return ResultDTOBuilder.failure(e.getLocalizedMessage());
        }

        if (i > 0){
            return ResultDTOBuilder.success("添加成功");
        }
        else {
            return ResultDTOBuilder.failure("添加失败");
        }
    }

    /**
     * 账号设置
     * */
    @PostMapping("/accountSettings")
    public ResultDTO accountSettings(HttpServletRequest request){
        Doctor loginDoctor;
        try {
            loginDoctor = doctorService.getLoginDoctor(request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (loginDoctor == null){
            return ResultDTOBuilder.failureMsg("登录已过期");
        }

        KeyUtils keyUtils = new KeyUtils();
        String password;
        try {
            password = keyUtils.desDecrypt(loginDoctor.getPassword(), null);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg("查询错误");
        }

        loginDoctor.setPassword(password);

        loginDoctor = doctorService.accountSettings(loginDoctor);

        return ResultDTOBuilder.success(loginDoctor);
    }
}